package com.eorchis.module.usermessagemanage.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.service.impl.AbstractBaseService;
import com.eorchis.core.ui.commond.ICommond;
import com.eorchis.module.basedata.domain.BaseData;
import com.eorchis.module.basedata.service.cache.BaseDataCacheUtil;
import com.eorchis.module.usermessagemanage.dao.IUserMessageManageDao;
import com.eorchis.module.usermessagemanage.domain.ExportUserMessageManageBean;
import com.eorchis.module.usermessagemanage.domain.UserCapacityBean;
import com.eorchis.module.usermessagemanage.domain.UserMessageManageBean;
import com.eorchis.module.usermessagemanage.service.IUserMessageManageService;
import com.eorchis.module.usermessagemanage.ui.commond.UserMessageManageQueryCommond;
import com.eorchis.utils.utils.PropertyUtil;


//声明本类为Server实现类，声明bean名称
@Service(value = "com.eorchis.module.usermessagemanage.service.impl.UserMessageManageServiceImpl")
public class UserMessageManageServiceImpl extends AbstractBaseService implements IUserMessageManageService {

	// 培训班信息接口
	@Autowired
	@Qualifier("com.eorchis.module.usermessagemanage.dao.impl.UserMessageManageDaoImpl")
	private IUserMessageManageDao userMessageManageDao;
	
	@Resource(name = "com.eorchis.module.basedata.service.cache.BaseDataCacheUtil")
	private BaseDataCacheUtil baseDataCacheUtil;
	
	@Override
	public IDaoSupport getDaoSupport() {
		return userMessageManageDao;
	}

	@Override
	public <C extends ICommond> C toCommond(IBaseEntity arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UserMessageManageBean> findUserList(UserMessageManageQueryCommond queryCommond) throws Exception {
		List<UserMessageManageBean> userList = userMessageManageDao.findUserList(queryCommond);
		if (PropertyUtil.objectNotEmpty(userList)) {
			Map<String, BaseData> baseDataMap =  baseDataCacheUtil.getBaseData();
			List<Integer> userIds = new ArrayList<Integer>();
			for (UserMessageManageBean userMessageManageBean : userList) {
				userIds.add(userMessageManageBean.getUserID());
			}
			queryCommond.setUserId(userIds);
			List<UserCapacityBean> capacityBeans = userMessageManageDao.findCodes(queryCommond);

			for(Integer id:userIds){
				String statusCodes="(";
				for(UserCapacityBean bean:capacityBeans){
					if((id.toString()).equals(bean.getUserId())){
						statusCodes+=(baseDataMap.get(bean.getDataId()).getDataName())+",";
					}
				}
				if(statusCodes.length()>1){
					for (UserMessageManageBean userMessageManageBean : userList) {
						if (userMessageManageBean.getUserID().equals(id)) {
							userMessageManageBean.setStatusCodes(statusCodes.substring(1, statusCodes.length()-1));
						}
					}
				}
			}
		}
		
		
		return userList;
	}

	public UserMessageManageBean getUserMessageManageBean(Integer userID) {
		return userMessageManageDao.getUserMessageManageBean(userID);
	}
	/**
	 * 修改
	 * @param queryCommond
	 * 韩向宇 2018-04-02
	 */
	public void updateUserMessageManage(UserMessageManageQueryCommond queryCommond) {
		userMessageManageDao.updateUserMessageManage(queryCommond);
	}
	/**
	 * 导出list
	 * @param queryCommond
	 * @return
	 */
	public List<ExportUserMessageManageBean> export(UserMessageManageQueryCommond queryCommond){
		return userMessageManageDao.export(queryCommond);
	}
	
}
